IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_HP_DiaChi]') AND type in (N'P', N'PC'))
   DROP PROCEDURE [dbo].[prc_HP_DiaChi]

GO


Create Proc prc_HP_DiaChi

@Action varchar(200) = '',
@Date_Add smalldatetime = '2011/03/15',
@Date_Up smalldatetime = '2011/03/15',
@DiaChi nvarchar(200) = '',
@DienGiai nvarchar(200) = '',
@HVTTLineRef varchar(100) = ''output,
@Scrn_Add nvarchar(50) = '',
@Scrn_Up nvarchar(50) = '',
@User_Add nvarchar(50) = '',
@User_Up nvarchar(50) = '',
@Sys_Stamp varchar(200) output

As

BEGIN

   If(@Action = 'Add')
   Begin
       Select @HVTTLineRef = Convert(varchar(100), NewID()) + '_' + Convert(varchar(30),GetDate(),21)
       Insert Into HP_DiaChi
      (
       Date_Add,Date_Up,DiaChi,DienGiai,HVTTLineRef,Scrn_Add,Scrn_Up,User_Add,User_Up
      )       Values
        (
     @Date_Add,@Date_Up,@DiaChi,@DienGiai,@HVTTLineRef,@Scrn_Add,@Scrn_Up,@User_Add,@User_Up
        )       If @@ROWCOUNT = 1
       Begin
           Select @Sys_Stamp =  master.dbo.fn_varbintohexstr(Sys_Stamp) from HP_DiaChi where HVTTLineRef = @HVTTLineRef
       End
   End
   If(@Action = 'Update')
   Begin
       Update HP_DiaChi Set Date_Add = @Date_Add,Date_Up = @Date_Up,DiaChi = @DiaChi,DienGiai = @DienGiai,HVTTLineRef = @HVTTLineRef,Scrn_Add = @Scrn_Add,Scrn_Up = @Scrn_Up,User_Add = @User_Add,User_Up = @User_Up       where HVTTLineRef = @HVTTLineRef
       If @@ROWCOUNT = 1
       Begin
           Select @Sys_Stamp =  master.dbo.fn_varbintohexstr(Sys_Stamp) from HP_DiaChi where HVTTLineRef = @HVTTLineRef
       End
   End
   If(@Action = 'UpdateAll')
   Begin
       Update HP_DiaChi Set Date_Add = @Date_Add,Date_Up = @Date_Up,DiaChi = @DiaChi,DienGiai = @DienGiai,HVTTLineRef = @HVTTLineRef,Scrn_Add = @Scrn_Add,Scrn_Up = @Scrn_Up,User_Add = @User_Add,User_Up = @User_Up       where HVTTLineRef like @HVTTLineRef
   End
   If(@Action = 'Delete')
   Begin
       Delete HP_DiaChi where HVTTLineRef = @HVTTLineRef
   End
   If(@Action = 'DeleteAll')
   Begin
       Delete HP_DiaChi where HVTTLineRef like @HVTTLineRef
   End
   If(@Action = 'GetAll')
   Begin
       Select  Date_Add,Date_Up,DiaChi,DienGiai,HVTTLineRef,Scrn_Add,Scrn_Up,Sys_Stamp = master.dbo.fn_varbintohexstr(Sys_Stamp),User_Add,User_Up, IsChange = 0 from HP_DiaChi where HVTTLineRef like @HVTTLineRef
   End
   If(@Action = 'GetByKey')
   Begin
       Select  Date_Add,Date_Up,DiaChi,DienGiai,HVTTLineRef,Scrn_Add,Scrn_Up,Sys_Stamp = master.dbo.fn_varbintohexstr(Sys_Stamp),User_Add,User_Up, IsChange = 0 from HP_DiaChi where HVTTLineRef = @HVTTLineRef
   End
END
GO




